package com.getpebble.android.framework.endpoint;

import com.getpebble.android.PebbleApplication;
import com.getpebble.android.bluetooth.protocol.ProtocolMessage;
import com.getpebble.android.common.core.trace.Trace;
import com.getpebble.android.common.model.FrameworkState;
import com.getpebble.android.framework.install.app.AppUpdateManager;
import com.getpebble.android.framework.protocol.EndpointId;
import com.getpebble.android.framework.protocol.inbound.PblInboundPingMessage;
import com.google.common.collect.ImmutableSet;
import java.util.Set;

public class PingEndpoint extends RequestableEndpoint {
    private final String TAG = getClass().getSimpleName();

    Set<EndpointId> getSupportedEndpoints() {
        return ImmutableSet.of(EndpointId.PING);
    }

    boolean onReceive(ProtocolMessage message) {
        PblInboundPingMessage pingMessage = new PblInboundPingMessage(message);
        Trace.debug(this.TAG, "Receieved ping message. isIdle = " + pingMessage.isIdle());
        if (pingMessage.isIdle()) {
            AppUpdateManager updateManager = AppUpdateManager.getInstance();
            if (updateManager != null) {
                updateManager.checkForUpdates(PebbleApplication.getAppContext().getContentResolver(), PebbleApplication.getConnectedDevice());
            } else {
                Trace.error(this.TAG, "Not Checking For Updates: Update Manager instance was null.");
            }
        }
        return true;
    }

    boolean onRequest(EndpointRequest request, FrameworkState frameworkState) {
        return false;
    }

    boolean onPrfRequest(EndpointRequest request, FrameworkState frameworkState) {
        return false;
    }
}
